<?php

namespace app;

use app\model\Admin;
use Closure;
use think\Request;

class AuthorizationMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $author = $request->header("authorization");
        if (empty($author)) {
            return response(json_encode(['errMsg' => '请登录', 'errCode' => 7004], 320), 401);
        }
        $author = json_decode($author, true);
        $admin = app(name: Admin::class)->find($author['id']);
        if (empty($admin)) return response(json_encode(['errMsg' => '请登录', 'errCode' => 7004], 320), 401);
        if ($admin->state != 1)
            return response(json_encode(['errMsg' => '账号已被禁用', 'errCode' => 7004], 320), 401);
        $timer = strtotime($admin->verify_time);
        if ($timer < time())
            return response(json_encode(['errMsg' => '登录已过期', 'errCode' => 7004], 320), 401);
        return $next($request);
    }
}
